var mysql = require('mysql'),
    http = require('http'),
    url = require('url'),
    crypto = require('crypto'),
    querystring = require('querystring'),
    config = require('./config.js');

function weibo(id) {
    var client = createMySQLClient();
    var sql = "select nickname, content, created_time, device from weibo limit 10;";
    client.query(sql, function(err, data) {
        if (err) {
            log('读取玩法配置出错：' + err.message);
            return false;
        } else {
            data.forEach(function(v) {
                log(v);
            });
        }
    });
    client.end();
}

weibo();

function createMySQLClient() {
    var conn = mysql.createConnection(config.dbinfo);
    conn.connect(function(error, results) {
        if (error) {
            console.log('Connection Error: ' + error.message);
            return;
        }
        console.log('Connected to MySQL');
    });
    return conn;
}

// var Db = {
//     mConn: {},
//     mTable: {},
//     mRows: 15,
//     init: function() {
//         var self = this;
//         self.mRows = 15;
//         self.getConn();
//         self.mTable = {};
//     },
//     getConn: function() {
//         var self = this;
//         var config = Config.getDbConfig();
//         var dbHost = config.dbHost;
//         var dbPort = config.dbPort;
//         var dbPassword = config.dbPassword;
//         var dbDatabase = config.dbDatabase;

//         var conn = new PDO('mysql:host=' + dbHost + ';port=' + dbPort + ';dbname=' + dbDatabase + ';charset=utf8', dbUser, dbPassword);
//         if (!conn) throw "数据库服务器忙碌";
//         conn.setAttribute(PDO.ATTR_ERRMODE, PDO.ERRMODE_EXCEPTION);
//         conn.setAttribute(PDO.ATTR_EMULATE_PREPARES, false);
//         conn.exec("SET NAMES utf8;");
//         self.mConn = conn;
//     }
// }